
<!DOCTYPE html>
<html xmlns:th="http://www.w3.org/1999/xhtml">
<head>
    <meta charset="UTF-8">
    <title>元数据</title>
    <link href="/static/devtools30/layui-v2.5.6/css/layui.css" rel="stylesheet" />
    <link href="/static/devtools30/css/devindexbase.css" rel="stylesheet" />
    <link href="/static/devtools30/layui-v2.5.6/ext/soulTable/soulTable.css" rel="stylesheet"/>

    <script src="/static/devtools30/js/jquery-1.11.2.min.js" type="text/javascript"></script>
    <script src="/static/devtools30/layui-v2.5.6/layui.all.js" type="text/javascript"></script>
    <script src="/static/devtools30/layui-v2.5.6/layui.js" type="text/javascript"></script>
    <script src="/static/devtools30/js/devtoolCommon.js" type="text/javascript"></script>
    <link href="/static/devtools30/js/plugins/codemirror-5.14.2/theme/eclipse.css" rel="stylesheet" type="text/css" />
    <link href="/static/devtools30/js/plugins/codemirror-5.14.2/lib/codemirror.css" rel="stylesheet" />
    <script src="/static/devtools30/js/plugins/codemirror-5.14.2/lib/codemirror.js" type="text/javascript"></script>
    <script src="/static/devtools30/js/plugins/codemirror-5.14.2/mode/sql/sql.js" type="text/javascript"></script>
    <script src="/static/devtools30/js/plugins/codemirror-5.14.2/mode/javascript/javascript.js" type="text/javascript"></script>

    <style>
        /*顶部容器*/
        .tophead {
            height: 32px;
            border-bottom: 1px solid #F7F7F7;
            padding: 0 5px;
        }
        /*顶部输入框，下拉框去掉layui默认边距*/
        .tophead .layui-form-item {
            margin-bottom: 0px!important;
        }
        /*顶部输入框，下拉框左边文本样式*/
        .tophead .layui-form-label {
            padding: 0;
            line-height: 25px;
            width: auto;
        }
        /*顶部form块样式*/
        .tophead .layui-input-block {
            margin-left: 34px;
            min-height: 26px;
        }
        /*顶部输入框，下拉框设置高度*/
        .tophead .layui-input,
        .layui-select {
            height: 25px;
        }
        /*顶部下拉框设置子项高度*/
        .tophead .layui-form-select dl {
            top: 26px;
            padding: 0;
        }
        /*顶部下拉框设置子项内容高度*/
        .tophead .layui-form-select dl dd,
        .layui-form-select dl dt {
            line-height: 24px;
        }
        .tophead .layui-form-item {
            width: 165px;
            padding: 5px;
        }
        /*顶部下拉框图标大小*/
        .tophead .layui-form-select .layui-edge {
            border-width: 4px;
        }
        /*搜索框左边距*/
        .tophead .search .layui-input-block {
            margin-left: 32px;
        }

        /*表格-----------------------------------*/

        /*表格字体大小*/
        .layui-table td,
        .layui-table th {
            font-size: 11px!important;
        }
        /*表格边距*/

        .layui-table,
        .layui-table-view {
            margin: 0;
        }
        /*按钮高度大小*/
        .layui-btn {
            height: 26px;
            line-height: 26px;
            font-size: 11px;
        }
        /*按钮组-------------------------------*/
        /*按钮组左边框*/
        .layui-btn-group .layui-btn-primary:first-child {
            border-left: 1px solid #c9c9c9!important;
        }
        /*按钮组边框*/
        .layui-btn-group .layui-btn {
            border-left: 1px solid rgba(255, 255, 255, .5)!important;
        }
    </style>

</head>
<body>
<div style="height: 100%;background: #F7F7F7;">
    <div class="flex flex-align-center flex-jusity-betreen tophead backgroundf">
        <div class="flex flex-align-center">
            <form class="layui-form" lay-filter="formTest">
                <div class="layui-form flex flex-align-center" >
                    <div class="layui-form-item marginleft10 search">
                        <label class="layui-form-label">查询</label>
                        <div class="layui-input-block">
                            <input type="text" id="searchValue"  name="searchValue" placeholder="请输入指标分类代码/指标分类名称" autocomplete="off" class="layui-input">
                        </div>
                    </div>
                    <button type="button" onclick="getZhibiaoList()" class="layui-btn layui-btn-sm layui-btn-normal marginleft10">确定</button>
                </div>
            </form>
        </div>
        <div class="layui-btn-group btns">
            <button type="button" class="layui-btn layui-btn-primary layui-btn-sm" name='101'>新增指标分类</button>
        </div>
    </div>
    <div class="flex flex-jusity-betreen " style="height:100%;width: 100%;background: #F7F7F7;padding: 3px 0;">
        <div class="backgroundf">
            <table  id="mygrid" lay-filter="mygrid"></table>
        </div>
        <div class="backgroundf" >
            <table  id="mygrid2"  lay-filter="mygrid2"></table>
        </div>
        <div class="backgroundf" style="width: 39.5%;padding: 5px;" >
            <form class="layui-form" lay-filter="zbform" >
                <input type="hidden" name="id" id ="zbidid" />
                <input type="hidden" name="xh" id ="xh" />
                <div class="layui-form-item">
                    <label class="layui-form-label">指标分类</label>
                    <div class="layui-input-block">
                        <select name="zbclassid" id="zbclassid" >
                            <option th:each="c : ${zbclassList}" th:value="${c.id}" th:text="${c.zbclassname}" >
                            </option>
                        </select>
                    </div>
                </div>
                <div class="layui-form-item">
                    <label class="layui-form-label">指标代码</label>
                    <div class="layui-input-block">
                        <input type="text" id="zbcode" name="zbcode" readonly="true" placeholder="保存时自动生成" autocomplete="off" class="layui-input">
                    </div>
                </div>
                <div class="layui-form-item">
                    <label class="layui-form-label">存储表名</label>
                    <div class="layui-input-block">
                        <input type="text" id="tablename" name="tablename" readonly="true" placeholder="保存时自动生成" autocomplete="off" class="layui-input">
                    </div>
                </div>
                <div class="layui-form-item">
                    <label class="layui-form-label">指标名称</label>
                    <div class="layui-input-block">
                        <input type="text" id="zbname" name="zbname"  lay-verify="required" placeholder="请输入" autocomplete="off" class="layui-input">
                    </div>
                </div>
                <div class="layui-form-item">
                    <div class="layui-inline">
                        <label class="layui-form-label">指标单位</label>
                        <div class="layui-input-inline">
                            <input type="text" id="zbunit" name="zbunit"  placeholder="请输入" autocomplete="off" class="layui-input">
                        </div>
                    </div>
                    <div class="layui-inline">
                        <label class="layui-form-label">小数位数</label>
                        <div class="layui-input-inline layui-input-wrap">
                            <input type="number" id="xsws" name="xsws"  placeholder="小数位数" autocomplete="off" class="layui-input">
                        </div>
                    </div>
                </div>
                <div class="layui-form-item">
                    <label class="layui-form-label">指标意义</label>
                    <div class="layui-input-block">
                        <textarea placeholder="请输入内容" id="zbmeans" name="zbmeans" class="layui-textarea"></textarea>
                    </div>
                </div>
                <div class="layui-form-item">
                    <label class="layui-form-label">指标备注</label>
                    <div class="layui-input-block">
                        <textarea placeholder="请输入内容" id="zbbz" name="zbbz"  class="layui-textarea"></textarea>
                    </div>
                </div>
                <div class="layui-form-item">
                    <label class="layui-form-label">指标SQL</label>
                    <div class="layui-input-block">
                        <textarea placeholder="请输入内容" id="zbsql" name="zbsql"   class="layui-textarea"></textarea>
                        <span style="color: red;">
                            只要填写SQL片段,示例：sum([测试主表.测试小数])
                            <a onclick="selModelField()">[弹窗选择]</a>&nbsp;
                            <a onclick="sqlPriview()">[SQL预览]</a>
                        </span>
                    </div>
                </div>
                <div class="layui-form-item">
                    <label class="layui-form-label">指标对象</label>
                    <div class="layui-input-block">
                        <input type="text" id="zhibiaoobjinfo" name="zhibiaoobjinfo" value=" " readonly="true" autocomplete="off" class="layui-input">
                    </div>
                </div>
                <div class="layui-form-item">
                    <div class="layui-input-block">
                        <button type="button" class="layui-btn layui-btn-normal" onclick="addNewZb()">重置清空</button>
                        <button type="button" class="layui-btn" onclick="submitZbForm()">立即提交</button>
                    </div>
                </div>
            </form>
        </div>
    </div>

</div>

<script>
    var height = dlCommon.getWinHeight();
    var width=dlCommon.getWinWidth();
    var selZbcalssId="";
    var editor = CodeMirror.fromTextArea(document.getElementById("zbsql"), {
        lineNumbers: true,
        matchBrackets: true,
        lineWrapping:true,
        mode: "text/x-plsql"
    });
    editor.setOption("theme", "eclipse");
    editor.setSize('90%', '200px');

    layui.config({
        base: '/static/devtools30/layui-v2.5.6/ext/',// 第三方模块所在目录
        version: 'v1.5.10' // 插件版本号
    }).extend({
        soulTable: 'soulTable/soulTable',
        tableChild: 'soulTable/tableChild',
        tableMerge: 'soulTable/tableMerge',
        tableFilter: 'soulTable/tableFilter',
        excel: 'soulTable/excel',
        dltable: 'treegrid/dltable',
        treeGrid:'treegrid/treeGrid'
    });
    layui.use(['form', 'table', 'jquery','treeGrid','layer','soulTable'], function(){
        $(function() {

            initGrid();

            initEvent();

            bindForm();

            getZbClassList();

        })
    });


    //数据表
    function getZbClassList(){

        var data={
            serach:$("#searchValue").val()
        };

        dlCommon.getJSON("/DevtZbclassInfoEntityController/getAll",data,function(res){
            layui.treeGrid.render({
                id:"mygrid"
                ,elem: '#mygrid'
                ,height: height-36
                , width: width*0.20
                , size: 'sm'//小尺寸的表格
                ,idField:'id'
                ,data:res&&res.data?(res.data):[]
                ,treeId:'id'//树形id字段名称
                ,treeUpId:'parentid'//树形父id字段名称
                ,treeShowName:'zbclassname'//以树形式显示的字段
                ,cols: [
                    [
                        {field:'zbclassname',width:140, title: '分类名称'},
                        {field:'zbclasscode', edit:'text',width:140, title: '分类代码'},
                        { field: 'opera', title: '操作', width: 120,templet: function(d){
                                var str="";
                                str += '<button type="button" onclick="editZbClass(\''+d.id+'\')" class="layui-btn layui-btn-normal layui-btn-xs layui_edit_btn">修改</button>';
                                str += '<button type="button" onclick="delZbClass(\''+d.id+'\')" class="layui-btn layui-btn-danger layui-btn-xs layui_edit_btn">删除</button>';
                                return str;
                            }
                        }
                    ]]
                ,page:false
            });
        });
        //单击表格
        layui.treeGrid.on('row(mygrid)', function(obj){
            selZbcalssId=obj.data.id;
            getZhibiaoList();
        });
    }

    function editZbClass(id){
        dlCommon.layeropen(600,400,'修改指标分类','/DevtZbclassInfoEntityController/edit?zbclassid='+id,{
            end:function () {
                bindForm();
                getZbClassList();
                getZhibiaoList();
            }
        })
    }

    function delZbClass(id){
        dlCommon.layerConfirm("是否确定删除该指标分类？",function(){
            dlCommon.getJSON("/DevtZbclassInfoEntityController/delete",{id:id},function(res){
                if(!res.success){
                    dlCommon.layerError(res.msg);
                    return;
                }
                getZbClassList();
                getZhibiaoList();
            })
        })
    }

    //元数据字段
    function getZhibiaoList(){
        if(!selZbcalssId){
            return;
        }
        var data={
            zbcalssId:selZbcalssId,
            serach:$("#searchValue").val()
        };
        layui.table.reload("mygrid2",{
            url:"/DevtZbcfgInfoEntityController/getAll",
            where:data,
            loading:true,
        });

    }

    function bindForm(){

        layui.form.render();
    }
    //初始化grid
    function initGrid(){

        //第2个实例
        layui.table.render({
            elem: '#mygrid2',
            height: height-40,
            width: width*0.40,
            size: 'sm', //小尺寸的表格
            loading:false,
            page: false, //开启分页
            limit: 30,
            limits: [30,60,90],
            cols: [
                [ //表头
                    { field: 'xh', title: '序号', sort: true, width: 70, },
                    { field: 'zbname', title: '指标名称', sort: true, width: 150, filter: true,},
                    { field: 'zbcode', title: '指标代码', sort: true, width: 150, filter: true, },
                    { field: 'createtime', title: '创建时间', sort: true, width: 150 ,templet: function(d){
                            var str=new Date(d.createtime).Format("yyyy-MM-dd HH:mm:ss");
                            return str;
                        }},
                    { field: 'updatetime', title: '修改时间', sort: true, width: 150 ,templet: function(d){
                            var str=new Date(d.updatetime).Format("yyyy-MM-dd HH:mm:ss");
                            return str;
                    }},
                    { field: 'opera', title: '操作', width: 80,templet: function(d){
                            var str="";
                            str += '<button type="button" onclick="delField(\''+d.id+'\')" class="layui-btn layui-btn-danger layui-btn-xs layui_edit_btn">删除</button>';
                            return str;
                        }
                    }
                ]
            ],
            filter: {
                bottom: false
            },
            done: function() {
                // 在 done 中开启
                layui.soulTable.render(this)
            }
        });
        layui.table.on('rowDouble(mygrid2)', function(obj){
            var objValue={};
            objValue.id=obj.data.id;
            objValue.xh=obj.data.xh;
            objValue.zbclassid=obj.data.zbclassid;
            objValue.zbcode=obj.data.zbcode;
            objValue.zbname=obj.data.zbname;
            objValue.zbunit=obj.data.zbunit;
            objValue.xsws=obj.data.xsws;
            objValue.zbmeans=obj.data.zbmeans;
            objValue.zbbz=obj.data.zbbz;
            objValue.zbsql=obj.data.zbsql;
            objValue.tablename=obj.data.tablename;
            editor.setValue(objValue.zbsql);
            layui.form.val("zbform",objValue);
            getzhibiaoobjinfo(obj.data.modelid);
        });
    }
    
    function getzhibiaoobjinfo(modelid) {
        dlCommon.getJSON("/DevtZbobjFieldEntityController/getAllByModelId",{modelid:modelid},function(res){
            if(!res.success){
                dlCommon.layerError(res.msg);
                return;
            }
            var formvalue1 = layui.form.val("zbform");
            formvalue1.zhibiaoobjinfo=res.data;
            layui.form.val("zbform",formvalue1);
        })
    }
    
    //初始化事件
    function initEvent(){
        //按钮点击
        $('.btns>button').on('click', function() {
            var names = $(this)[0].name;
            if(names == '101') {
                dlCommon.layeropen(600,400,'新增指标分类','/DevtZbclassInfoEntityController/edit',{
                    end:function () {
                        bindForm();
                        getZbClassList();
                        getZhibiaoList();
                    }
                })
                return;
            }

        })

    }

    function selModelField() {
        dlCommon.layeropen(800,600,'选择模型及字段','/DevtZbcfgInfoEntityController/selModelField',{

        });
    }
    function selTableField(tablename,fieldName,tableid) {
        var relation="["+tablename+"."+fieldName+"]";
        var content=editor.getValue();
//       editor.setValue(content+' '+relation);
        editor.replaceSelection(relation);
        editor.focus();
        getzhibiaoobjinfo(tableid)
    }

    function sqlPriview(){
        var content=editor.getValue();
        dlCommon.layeropen(680,480,'SQL预览','/DevtZbcfgInfoEntityController/sqlPriview?zbsql='+encodeURI(encodeURI(content)),{

        });
    }

    function delField(id) {
        if(!id){
            return;
        }
        dlCommon.layerConfirm("是否确定删除该指标？",function(){
            dlCommon.getJSON("/DevtZbcfgInfoEntityController/delete",{id:id},function(res){
                if(!res.success){
                    dlCommon.layerError(res.msg);
                    return;
                }
                getZhibiaoList();
            });
        })

    }

    function addNewZb() {
        var objValue={};
        objValue.id='';
        objValue.xh='';
        objValue.zbclassid='';
        objValue.zbcode='';
        objValue.zbname='';
        objValue.zbunit='';
        objValue.xsws='';
        objValue.zbmeans='';
        objValue.zbbz='';
        objValue.zbsql='';
        objValue.tablename='';
        editor.setValue('');
        layui.form.val("zbform",objValue);
    }

    function submitZbForm(){
        var formvalue1 = layui.form.val("zbform");
        formvalue1.zbsql=editor.getValue();
        console.log(formvalue1);
        layer.confirm('确定提交吗？', function(index){
            layer.close(index);

            dlCommon.getJSON("/DevtZbcfgInfoEntityController/save",formvalue1,function(res){
                if(!res.success){
                    dlCommon.layerError(res.msg);
                    return;
                }
                getZhibiaoList();
            })
        });
    }

</script>
</body>
</html>
